<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


if (!function_exists('p')) {

    function p($arr, $exit=false) {
        echo '<pre>';
        print_r($arr);
        echo '</pre>';
        if ($exit)
            exit('bye');
    }

}

if (!function_exists('generate_attributes')) {

    function generate_attributes($param, $exceptions = array('selected', 'options', 'label')) {
        $ret = ' ';
        if($param){
            foreach ($param as $i => $v) {
                if (!in_array($i, $exceptions))
                    $ret .= $i . '="' . $v . '" ';
            }    
        }
        
        return $ret;
    }

}



if (!function_exists('e')) {

    function e($param) {
        echo $param;
    }

}

if (!function_exists('img')) {

    function img($file, $attr = array()) {
        return '<img ' . generate_attributes(array_merge($attr, array('src' => IMAGES_PATH . $file))) . '>';
    }

}



if(!function_exists('anc')){
    function anc($link='', $label='', $attribute=''){
        $ret = '';
        $attr = array('href'=>BASE_URL.$link);
        if($attribute){
            $attr = array_merge($attr, $attribute);
        }
        
        return '<a '. generate_attributes($attr) .'>'.$label.'</a>';
    }
}

if(!function_exists('frm')){
    function frm($action='', $attribute=''){
        $ret = '';
        $attr = array('action'=>BASE_URL.$action);
        if($attribute){
            $attr = array_merge($attr, $attribute);
        }
        
        return '<form '. generate_attributes($attr) .'>';
    }
}

if (!function_exists('server')) {

    function server() {
        echo '<table>';
        foreach ($_SERVER as $i => $v) {
            echo '<tr><td>' . $i . ':</td><td>' . $v . '</td></tr>';
        }
        echo '</table>';
    }
}

if(!function_exists('js')){
    function js($src,$body=''){
        return '<script type="text/javascript" src="'.$src.'.js">'.$body.'</script>';
    }
}

if(!function_exists('css')){
    function css($href){
        return '<link href="'.$href.'.css" rel="stylesheet" type="text/css" />';
    }
}

if(!function_exists('display_option')){
    function display_option($controller, $data, $index){
        
        //($i2=='code')?htmlentities($v[$i2]):$v[$i2]
        $ret = $data[$index];    
        
        switch ($controller.':'.$index){
            case'banners:code':
                $ret = htmlentities($ret);
                break;
            case 'files:location':
                $dimention = '';
                if($data['type']=='picture'){
                    $attr = getimagesize(DOC_ROOT.$data['location']);
                    $dimention = ' | '.$attr[0].' x '.$attr[1];
                }
                $ret = '<img src="'.BASE_URL.$data['thumbnail'].'" class="curb" />';
                $ret .= '<div>'.BASE_URL.$data['location'].$dimention.'</div>';
                break;
            case 'pages:url':
                $url = BASE_URL . $data[$index];
                $ret = '<a href="'.$url.'" target="blank">'.$url.'</a>';
                break;
            default :
                //$ret = $data[$index];
                break;
        }
        
        return $ret;
    }
}